package com.example.mybatis.sqlserver.springbootmybatissqlserver.controller;

import com.example.mybatis.sqlserver.springbootmybatissqlserver.pojo.dto.*;
import com.example.mybatis.sqlserver.springbootmybatissqlserver.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by hzy on 2019/2/19.
 */

@RestController
@RequestMapping("/api/Category")
public class CategoryController {

    @Autowired
    ICategoryService categoryService;

    @PostMapping("/AddCategory")
    public WebResultBean<GetCategoryDTO> addCategory(@Validated @RequestBody AddCategoryDTO dto){
        return categoryService.addCategory(dto);
    }

    @PostMapping("/UpdateCategory")
    public WebResultBean<Boolean> updateCategory(@Validated @RequestBody EditCategoryDTO dto){
        return categoryService.updateCategory(dto);
    }

    /**
     * 根据父类Id，获取子类集合.
     *
     * 目前只返回一级子类,用list结构。
     *
     * @param parentId
     * @return
     */
    @GetMapping("/GetCategories")
    public WebResultBean<List<GetCategoryDTO>> getCategories(@RequestParam("parentId") String parentId){
        return categoryService.getCategoriesByParentId(parentId);
    }


    /**
     * 根据父类Id，获取子类集合(包含子类的子类).
     *
     * 返回树形结构
     * @return
     */
    @GetMapping("/GetCategoryList")
    public WebResultBean<List<GetCategoryTreeDTO>> getCategoryList(){
        return categoryService.getCategoryTree();
    }

    /**
     * 根据id获取单个类目信息
     */
    @GetMapping("/GetCategory")
    public WebResultBean<GetCategoryDTO> getCategory(@RequestParam("categoryId") String categoryId){
        return categoryService.findCategory(categoryId);
    }


    /**
     * 根据删除id删除类目.
     *
     * 软删除
     */
    @PostMapping("/DelCategory")
    public WebResultBean<Boolean> deleteCategory(@Validated @RequestBody DeleteCategoryDTO dto){
        return categoryService.deleteCategory(dto);
    }


    /**
     * 添加类目属性
     * @param dto
     * @return
     */
    @PostMapping("/AddCategoryProperty")
    public WebResultBean<Boolean> addCategoryProperty(@Validated @RequestBody AddCategoryPropertyDTO dto){
        return categoryService.addCategoryProperty(dto);
    }

    /**
     * 获取类目属性
     */
    @GetMapping("/GetCategoryPropertyById")
    public WebResultBean<GetPropertyDTO> findCategoryPropertyById(@RequestParam("propertyId") String propertyId){
        return categoryService.findCategoryPropertyById(propertyId);
    }

    /**
     * 修改类目属性.
     * @param dto
     * @return
     */
    @PostMapping("/UpdateCategoryPropertyName")
    public WebResultBean<Boolean> editCategoryPropertyName(@Validated @RequestBody EditCategoryPropertyDTO dto){
        return categoryService.updateCategoryPropertyName(dto);
    }

    /**
     * 设置属性名是否显示.
     */
    @PostMapping("/CategoryPropertyNameIsShow")
    public WebResultBean<Boolean> setCategoryPropertyNameIsShow(@Validated @RequestBody SetCategoryPropertyNameIsShowDTO dto){
        return categoryService.setCategoryPropertyNameIsShow(dto);
    }

    /**
     * 获取类目属性集合.
     *
     * 获取该类目下的全部属性集合.
     */
    @GetMapping("/GetCategoryPropertyName")
    public WebResultBean<List<GetPropertyDTO>> listCategoryPropertyName(@RequestParam("categoryId") String categoryId){
        return categoryService.listCategoryPropertyName(categoryId);
    }

    /**
     * 添加类目属性值.
     */
    @PostMapping("/AddCateogryPropertyValue")
    public WebResultBean<Boolean> addCategoryPropertyValue(@Validated @RequestBody AddCategoryPropertyValueDTO dto){
        return categoryService.addCategoryPropertyValue(dto);
    }

    /**
     * 修改类目属性值信息.
     * @param dto
     * @return
     */
    @PostMapping("/UpdateCateogryPropertyValue")
    public WebResultBean<Boolean> editCategoryPropertyValue(@Validated @RequestBody EditCategoryPropertyValueDTO dto){
        return categoryService.editCategoryPropertyValue(dto);
    }


    /**
     * 设置属性值是否显示.
     */
    @PostMapping("/CategoryPropertyValIsShow")
    public WebResultBean<Boolean> setCategoryPropertyValueIsShow(@Validated @RequestBody SetCategoryPropertyValueIsShowDTO dto){
        return categoryService.setCategoryPropertyValueIsShow(dto);
    }

    /**
     * 根据属性名id获取属性值.
     */
    @GetMapping("/GetCategoeyPropertyVal")
    public WebResultBean<List<GetPropertyValueDTO>> getCategoryPropertyVal(@RequestParam("propertyId") String propertyId){
        return categoryService.listCategoryPropertyValue(propertyId);
    }

    // TODO: 2019/2/21 POST api/Category/CopyProperty
    // ??
}
